package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.mapper.DishesFlavorsMapper;
import com.sky.mapper.DishesMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishesService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: DishesServiceImpl
 * @Author: YuanDing
 * @Date: 2024/2/3 20:36
 * @Description:
 */

@Slf4j
@Service
public class DishesServiceImpl implements DishesService {

    @Autowired
    private DishesMapper dishesMapper;

    @Autowired
    private DishesFlavorsMapper dishesFlavorsMapper;



    /**
     * 获取菜品分页数据
     * @return
     */
    @Override
    public Result<PageResult> getDishesList(DishPageQueryDTO dishPageQueryDTO) {
        System.out.println("dishPageQueryDTO = " + dishPageQueryDTO);
        PageHelper.startPage(dishPageQueryDTO.getPage() , dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishesMapper.selectDishesByPageNumber(dishPageQueryDTO);
        log.info("page: {}" , page);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return Result.success(pageResult);
    }

    /**
     * 更新菜品状态
     * @param status
     */
    @Override
    public void startOrStop(Integer status , Long id) {
        Dish dish = Dish.builder()
                        .id(id)
                        .status(status)
                        .build();
        dishesMapper.updateDish(dish);
    }

    @Override
    public Result getDisheById(Long id) {
        Dish dish = dishesMapper.selectDishById(id);
        List<DishFlavor> flavors = dishesFlavorsMapper.selectFlavors(id.intValue());
        DishVO dishVO = new DishVO();
        if (dish != null) {
            BeanUtils.copyProperties(dish , dishVO);
            dishVO.setFlavors(flavors);
            return Result.success(dishVO);

        } else {
            throw new BaseException("菜品不存在");
        }
    }

    /**
     * 保存菜品
     * @param dishDTO
     */
    @Override
    public void saveDishes(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO , dish);

        dishesMapper.insertDish(dish);

        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (id != null) {
            flavors.forEach( item -> {
                item.setDishId(id);
            });
            dishesFlavorsMapper.insertBatch(flavors);
        }

    }

    /**
     * 根据分类id获取菜品
     * @return
     */
    @Override
    public Result getDishByCategoryId(Integer categoryId) {
        List<Dish> dishList = dishesMapper.selectDisheByCategoryId(categoryId);

        return Result.success(dishList);
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    public void removeDishBatch(String ids) {
        String[] idArr = ids.split(",");
        dishesMapper.deleteDishBatch(idArr);
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishesMapper.updateDish(dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            dishesFlavorsMapper.deleteDishFlavors(dish.getId());
            flavors.forEach( item -> {
                item.setDishId(dish.getId());
            });
            dishesFlavorsMapper.insertBatch(flavors);
        }


    }
}
